--------------------------------------------------------------------------------
-- G600 スクリプト（G15専用：硬直中連打無効化 ＆ チャージ1回中断対応版）
--------------------------------------------------------------------------------

-- 【重要】マウスのボタンイベントを確実に拾うための有効化
EnablePrimaryMouseButtonEvents(true)

-- ==========================================
-- スキル回し用設定（ミリ秒）
-- ==========================================
-- クールダウン (CT)
local CT_key1 = 8484
local CT_key2 = 10017
local CT_key3 = 3884
local CT_key4 = 12000
local CT_key5 = 13500

-- キャストタイム
local Cast_key1 = 1167
local Cast_key2 = 584
local Cast_key3 = 667
local Cast_key4 = 617
local Cast_key5 = 684

-- ディレイタイム（key1, key2, key4 用）
local Delay_key1 = 367
local Delay_key2 = 300
local Delay_key4 = 367

-- チャージフルチャージ到達時間
local ChargeMax_key3 = 2467
local ChargeMax_key5 = 3250

-- 状態管理用変数
local CD_key1 = 0
local CD_key2 = 0
local CD_key3 = 0
local CD_key4 = 0
local CD_key5 = 0

local CastEndTick = 0        -- 硬直終了予定時刻（またはチャージ最大受付許容時間）
local CastFinishTick = 0     -- キャストタイム終了予定時刻
local SkillExecuteTick = 0   -- スキルが実行された開始時刻
local last_skill = ""        -- 直前に使用したスキルを記録する変数
local charge_interrupted = false -- チャージ中断を1回行ったかどうかのフラグ


--------------------------------------------------------------------------------
-- イベントハンドラ
--------------------------------------------------------------------------------
function OnEvent(event, arg)
    local currentTick = GetRunningTime()

    -- ==================================================================
    -- 【マウスボタンが押されたイベント】
    -- ==================================================================
    if (event == "MOUSE_BUTTON_PRESSED") then
        
        -- ==================================================================
        -- 【ボタン 15 の処理】：スキル自動ローテーション ＆ チャージ中断
        -- ==================================================================
        if (arg == 15) then
            -- 硬直中（またはチャージ受付期間中）の処理
            if (currentTick < CastEndTick) then
                -- key3 または key5 の場合、まだ中断していなければ1回だけ中断を受け付ける
                if ((last_skill == "3" or last_skill == "5") and not charge_interrupted) then
                    local cast_time = (last_skill == "3") and Cast_key3 or Cast_key5
                    
                    -- 各スキルのキャストタイム未満の再入力（連打）は受け付けない
                    if (currentTick < SkillExecuteTick + cast_time) then
                        return
                    end

                    -- キャストタイム以上、かつフルチャージ到達時間未満であれば中断を受け付ける
                    SendSkillTwice(last_skill) -- もう一度キーを送ってチャージを打ち切る
                    charge_interrupted = true  -- 1回実行したフラグを立てる（2回目以降の連打は無視される）
                    CastEndTick = currentTick  -- 硬直を即座に終了
                    CastFinishTick = currentTick
                    return
                end

                -- その他のスキル、またはすでに中断済みの場合は、連打しても硬直を延長せず完全に無視（次のスキルへ進まない）
                return
            end

            -- 1. key1 の判定 (※ key4 の直後は使用不可)
            if (currentTick >= CD_key1 and last_skill ~= "4") then
                SendSkillTwice("1")
                CastFinishTick = currentTick + Cast_key1
                CastEndTick = currentTick + Cast_key1 + Delay_key1
                CD_key1 = currentTick + Cast_key1 + CT_key1
                last_skill = "1"
                charge_interrupted = false
                SkillExecuteTick = currentTick
                return
            end

            -- 3. key5 の判定（チャージスキル：フルチャージ到達は 3250ms）
            if (currentTick >= CD_key5) then
                SendSkillTwice("5")
                CastFinishTick = currentTick + Cast_key5
                CastEndTick = currentTick + ChargeMax_key5
                CD_key5 = currentTick + Cast_key5 + CT_key5
                last_skill = "5"
                charge_interrupted = false
                SkillExecuteTick = currentTick
                return
            end

            -- 2. key2 の判定 (※ key1 または key3 の直後は使用不可)
            if (currentTick >= CD_key2 and last_skill ~= "1" and last_skill ~= "3") then
                SendSkillTwice("2")
                CastFinishTick = currentTick + Cast_key2
                CastEndTick = currentTick + Cast_key2 + Delay_key2
                CD_key2 = currentTick + Cast_key2 + CT_key2
                last_skill = "2"
                charge_interrupted = false
                SkillExecuteTick = currentTick
                return
            end

            -- 4. key4 の判定
            if (currentTick >= CD_key4) then
                SendSkillTwice("4")
                CastFinishTick = currentTick + Cast_key4
                CastEndTick = currentTick + Cast_key4 + Delay_key4
                CD_key4 = currentTick + Cast_key4 + CT_key4
                last_skill = "4"
                charge_interrupted = false
                SkillExecuteTick = currentTick
                return
            end

            -- 5. key3 の判定（チャージスキル：フルチャージ到達は 2467ms）
            if (currentTick >= CD_key3) then
                SendSkillTwice("3")
                CastFinishTick = currentTick + Cast_key3
                CastEndTick = currentTick + ChargeMax_key3
                CD_key3 = currentTick + Cast_key3 + CT_key3
                last_skill = "3"
                charge_interrupted = false
                SkillExecuteTick = currentTick
                return
            end

            -- フォールバック: key5
            SendSkillTwice("5")
            CastFinishTick = currentTick + Cast_key5
            CastEndTick = currentTick + ChargeMax_key5
            CD_key5 = currentTick + Cast_key5 + CT_key5
            last_skill = "5"
            charge_interrupted = false
            SkillExecuteTick = currentTick
            return
        end

    end -- MOUSE_BUTTON_PRESSED の終了
end -- OnEvent の終了

--------------------------------------------------------------------------------
-- 補助関数
--------------------------------------------------------------------------------
function SendSkillTwice(key)
    PressKey(key)
    ReleaseKey(key)
end